package com.lmaos.basetools.base;

import java.io.Closeable;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lmaos.allcontrol.beancontrol.class_.ClassUtils;
import com.lmaos.allcontrol.beancontrol.field_.FieldUtils;

public class SBaseUtils {
	public static void close(AutoCloseable close){
		try {
			if(close!=null){
				close.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	public static void close(Closeable close){
		try {
			if(close!=null){
				close.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	/**
	 * 说明：简洁版的copy工具，只能复制一层,讲一层ParamMap复制到一个对象		<br>
	 * 时间：2017年6月19日 上午11:18:15
	 *
	 * @param params
	 * @param desc
	 * @param nameMapping
	 */
	@SuppressWarnings("rawtypes")
	public static void simpleCopySParamMap(SParamMap params,Object desc,Map<String, String> nameMapping){
		List<Field> fields = FieldUtils.getFields(desc);
		for (Field field : fields) {
			String key = field.getName();
			if(nameMapping!=null&&nameMapping.containsKey(key)){
				key = nameMapping.get(key);
			}
			
			Class type = field.getType();
			type = ClassUtils.getBaseType((Class)type);
			Object value = null;
			if(type == int.class){
				value = params.getInt(key);
			}else if(type == long.class){
				value = params.getLong(key);
			}else if(type == byte.class){
				value = params.getByte(key);
			}else if(type == short.class){
				value = params.getShort(key);
			}else if(type == float.class){
				value = params.getFloat(key);
			}else if(type == double.class){
				value = params.getDouble(key);
			}else if(type == boolean.class){
				value = params.getBoolean(key);
			}else if(type == String.class){
				value = params.getString(key);
			}else if(Date.class.isAssignableFrom(type)){
				value = params.getDate(key);
			}else if(type == BigDecimal.class){
				value = params.getBigDecimal(key);
			}else{ // 其他类型对象
				value = params.get(key);
			}
			FieldUtils.setFieldValue(desc, field, value);
		}
	}
	
	public static Map<String, String> getStringMap(Object...params){
		Map<String, String> map = new HashMap<String,String>();
		int length = params.length;
		if((params.length&1)!=0){
			length --;
		}
		for (int i = 0; i < length; i+=2) {
			if(params[i+1]!=null){
				String key = String.valueOf(params[i]);
				String value = String.valueOf(params[i+1]);
				map.put(key, value);
			}
		}
		return map;
	}
	
	public static SParamMap getSParamMap(Object...params){
		SParamMap map = new SParamMap();
		int length = params.length;
		if((params.length&1)!=0){
			length --;
		}
		for (int i = 0; i < length; i+=2) {
			String key = String.valueOf(params[i]);
			Object value = params[i+1];
			if(value!=null){
				map.put(key, value);
			}
		}
		return map;
	}
	public static <T>List<T> toSimpleTBObjects(List<SParamMap> datas,Class<T> type,Map<String, String> nameMapping){
		List<T> objs = new ArrayList<T>();
		for (SParamMap param : datas) {
			T obj = ClassUtils.getObject(type);
			SBaseUtils.simpleCopySParamMap(param, obj, nameMapping);
			objs.add(obj);
		}
		return objs;
	}
	
	public static String classPath(Class clazz){
		URL url = clazz.getClassLoader().getResource("");
		if(url == null){
			return null;
		}
		return url.getPath().replace("\\", "/");
	}
	public static String classPath2(Class clazz){
		try {
			String classPath = classPath(clazz);
			if(classPath==null){
				URL url = clazz.getResource("");;
				classPath = url.getPath();
				classPath = getFileString(classPath);
			}
			return classPath.replace("\\", "/");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public static String getFileAbsoluteString(String path){
		int fileStartIndex = path.indexOf("file:");
		if(fileStartIndex != -1){
			path = path.substring(fileStartIndex+"file:".length());
		}
		if(path.startsWith("/")){
			int panIndex = path.indexOf(":");
			if(panIndex>1&&panIndex<=3){
				path = path.substring(1);
			}
		}
		return path;
	}
	public static String getFileString(String path){
		path = getFileAbsoluteString(path);
		int paramIndex = path.lastIndexOf("?");
		if(paramIndex == -1){
			paramIndex = path.lastIndexOf("!");
		}
		if(paramIndex != -1){
			path = path.substring(0, paramIndex);
		}
		return path.trim();
	}
	
}
